<template>
	<view>
		<u-loading-page loading-text="loading..."></u-loading-page>
	</view>
</template>

<script>
	import {
		InformationBusinessCardList,
		InformationUserCodeLogin
	} from '@/common/http/api.js'
	export default {
		data() {
			return {
				shareId: '', // 点击用户进入
				userId: '', // 点击用户进入
				type: '', //1 分享视频 2榜单 3日志详情 4圈子详情 5商城 6名片夹
				courseId: '', //视频id
				video_id: '', //视频id
				id: '',
				params:{}
			};
		},
		onLoad(e) {
			console.log('初始化onLoad', e)
			
			
			if (e.id) {
				this.id = e.id
				this.$store.commit('setIsRefresh', true)
			}
			// 点击用户进入
			if (e.userId) {
				this.userId = e.userId
				this.$store.commit('setIsRefresh', true)
			}
			if (e.user_id) {
				this.userId = e.user_id
				this.$store.commit('setIsRefresh', true)
			}
			// 分享进入
			if (e.shareId) {
				this.shareId = e.shareId
				this.$store.commit('setIsRefresh', true)
			}
			if (e.type) {
				//分享视频
				this.type = Number(e.type)
				this.$store.commit('setIsRefresh', true)
				
				
				if(this.type === 5) {
					//视频
					this.params = e
				}
			}
			if (e.courseId) {
				this.courseId = e.courseId
				this.$store.commit('setIsRefresh', true)
			}
			if (e.video_id) {
				this.video_id = e.video_id
				this.$store.commit('setIsRefresh', true)
			}
			
			console.log('结束')
		},
		async onShow() {

			//默认领取code
			if (!uni.getStorageSync('token')) {
				await this.login()
			}


			console.log('初始化', this.type, this.id, this.userId)
			
			//判断名片数量
			const r = await InformationBusinessCardList()
			const cardList = r.data
			
			
			switch (this.type) {
				case 1:
					//分享视频
					uni.reLaunch({
						url: '/pagesB/teachingCourses/teachingCourses?id=' + this.courseId + '&type=1' +
							'&userId=' + this.userId + '&video_id=' + this.video_id
					})
					break;
				case 2:
					//判断是否领取名片
					const res = await InformationBusinessCardList()
					if (Array.isArray(res.data) && res.data.length === 0) {
						//没有名片
						uni.navigateTo({
							url: '/pagesC/index/addMyCard'
						})
					} else {
						//this.$store.dispatch('getAddress',2)
						uni.reLaunch({
							url: '/pagesA/popularityChart/popularityChart'
						})
					}
					break;
					// case 3:
					// 	break;
				case 4:
					//圈子详情
					uni.reLaunch({
						url: '/pages/circle/detail?userId=' + this.userId + '&id=' + this.id
					})
					break;
				case 5:
					
					let query = '?'
					
					for(let j in this.params) {
						query += `${j}=${this.params[j]}&`
					}
					//圈子详情
					uni.reLaunch({
						url: '/pagesA/shopping/detail' + query
					})
					
					
					break;
				default:
					//正常流程
					if (!(this.shareId || this.userId)) {
						if(Array.isArray(cardList) && cardList.length > 0) {
							//有名片
							uni.switchTab({
								url: '/pages/index/index?current=0'
							})
						} else {
							uni.reLaunch({
								url: '/pagesA/findInterest/findInterest?type=' + 1
							})
						}
						
					} else {
						//分享进入
						let params = `?isBackHome=1`
						if (this.userId) {
							params += `&userId=${this.userId}`
						}
						if (this.shareId) {
							params += `&shareId=${this.shareId}`
						}
						//this.$store.dispatch('getAddress',2)
						uni.reLaunch({
							url: '/pages/index/other' + params
						})
					}
					break;
			}
		},
		methods: {
			login() {
				return new Promise((res,rej)=>{
					uni.login({
						provider: 'weixin',
						success: loginRes => {
							let data = {
								code: loginRes.code
							}
							if (this.userId) {
								data['share_userId'] = this.userId
							}
							if (this.shareId) {
								data['login_share_business_card_id'] = this.shareId
							}
							InformationUserCodeLogin(data).then(d =>{
								uni.setStorageSync("token", d.data.token)
								res()
							}).catch(err=> {
								rej(err)
							})
						},
						fail(err) {
							rej(err)
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss">

</style>